import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild,
  ElementRef,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  ComboboxItemDto,
  EquipmentForNodeInput,
  EquipmentServiceProxy,
  GetCreateUserWorkingDto,
  GetUserWorkingDto,
  NameValueDto,
  NodeEquipmentDto,
  NodeToolingDto,
  OrganizationUnitServiceProxy,
  PipelineListDto,
  ProcViewDto,
  SetNodeEquipmentsInput,
  SetNodeToolingsInput,
  TeamGroupTaskServiceProxy,
  ToolingServiceProxy,
  UserWorkingTimeInput,
  UserWorkingTimeServiceProxy,
} from "@shared/service-proxies/service-proxies";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import {
  PipelineNodeServiceProxy,
  StationDto,
} from "@shared/service-proxies/service-proxies";

@Component({
  selector: "gxzx",
  templateUrl: "./gxzx.component.html",
  styleUrls: ["./gxzx.component.less"],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class GXZXModalComponent extends AppComponentBase {
  stationId = 0;
  station = new StationDto();
  stations = new Array<StationDto>();
  equipments = new Array<NodeEquipmentDto>();

  shebei: ComboboxItemDto[];
  selectshebei: ComboboxItemDto[];
  selectsb = new Array<EquipmentForNodeInput>();
  toolings = new Array<NodeToolingDto>();
  gongzhuang: ComboboxItemDto[];
  selectgongzhuang: ComboboxItemDto[];
  ishavevalue = true;
  newCar: boolean;
  saving = false;
  displayDialog: boolean;
  totalProcWorkingTime = 0;
  users = new Array<NameValueDto>();
  checked=false;
  setNodeEquipments = new SetNodeEquipmentsInput();
  pipe: PipelineListDto = new PipelineListDto();
  setNodeToolings = new SetNodeToolingsInput();
  procInfo = new ProcViewDto();

  nodeWorker = new Array<GetCreateUserWorkingDto>();
  newnodeWorker = new Array<GetUserWorkingDto>();
  userworkingtime = 0;
  userworkingtimestring = "0";
  constructor(
    injector: Injector,
    private _taskServiceProxy: PipelineNodeServiceProxy,
    private _equipmentServiceProxy: EquipmentServiceProxy,
    private _teamGroupTaskService: TeamGroupTaskServiceProxy,
    private _toolingServiceProxy: ToolingServiceProxy,
    private _userWorkingTimeServiceProxy: UserWorkingTimeServiceProxy
  ) {
    super(injector);
  }

  load(pipe: PipelineListDto): void {
    this.pipe = pipe;
    this.userworkingtime = 0;
    // this.nodeWorker = new Array<GetUserWorkingDto>();
    this.newnodeWorker = new Array<GetUserWorkingDto>();
    if(this.pipe.taskTypeName=="自制外协任务" || this.pipe.taskTypeName=="外协任务"){
      this.checked=true;
    }
    this.reloadProcInfo();
   
    this._userWorkingTimeServiceProxy
      .getNodeWorkers(pipe.nodeId)
      .subscribe((result) => {
        this.nodeWorker = result;
        console.log(this.nodeWorker);
        this.jisuan();
      });
    this._teamGroupTaskService
      .findUsers(this.pipe.mainTeamGroup, 1)
      .subscribe((result) => {
        this.users = result;
      });
    this._userWorkingTimeServiceProxy
      .getNodeWorkersByNodeid(pipe.nodeId)
      .subscribe((result) => {
        this.totalProcWorkingTime = result;
      });
  }


  reloadProcInfo() {
    this._taskServiceProxy.getProcInfo(this.pipe.id).subscribe((result) => {
      this.procInfo = result;
    });
  }
  selectgw() {
    this.stations.forEach((el) => {
      if (this.stationId === el.id) {
        this.station = el;
      }
    });
  }
  jisuan() {
    this.userworkingtime = 0;
    this.nodeWorker.forEach((item) => {
      this.userworkingtime = this.userworkingtime + item.personWorkingTime;
    });
    this.newnodeWorker.forEach((item) => {
      this.userworkingtime = this.userworkingtime + item.personWorkingTime;
    });
    this.userworkingtimestring = this.userworkingtime.toFixed(2);
  }
  showDialogToAdd() {
    let selected = new GetUserWorkingDto();
    selected.userName = new NameValueDto();
    selected.userName = this.users[0];
    selected.personWorkingTime = 0;
    this.newnodeWorker.push(selected);
  }
  save() {
    //////保存工时
    if (
      this.totalProcWorkingTime * this.procInfo.amountPlanned <
      this.userworkingtime
    ) {
      this.notify.error("分配工时不能大于总工时");
    } else {
      this.ishavevalue = false;
      let nodeWorkers = new UserWorkingTimeInput();
      nodeWorkers.getUserWorkings = this.newnodeWorker;
      nodeWorkers.totalQuotaWorkingTime = this.pipe.totalQuotaWorkingTime;
      nodeWorkers.nodeEntityId = this.pipe.nodeId;
      nodeWorkers.pipelineEntityId = this.pipe.id;
      this._userWorkingTimeServiceProxy
        .setNodeWorkers(nodeWorkers)
        .subscribe((result) => {
          this.newnodeWorker = new Array<GetUserWorkingDto>();
          this._userWorkingTimeServiceProxy
            .getNodeWorkers(this.pipe.nodeId)
            .subscribe((result) => {
              this.nodeWorker = result;
            });
          this.notify.success("保存成功");
        });
    }

    //this.addToolings();
  }

  onRowEditInit(data) {
    let nodeWorkers = new Array<GetUserWorkingDto>();
    for (let index = 0; index < this.newnodeWorker.length; index++) {
      //let nodeWorker = new GetNodeWorkOutput();
      if (index !== data) {
        nodeWorkers.push(this.newnodeWorker[index]);
      }
    }
    this.newnodeWorker = nodeWorkers;
  }
 
  start() {
    this._taskServiceProxy
      .componentProcStart(this.pipe.id)
      .subscribe((result) => {
        this.notify.success("工序开始成功");
        this.reloadProcInfo();
      });
  }

  pause() {
    this._taskServiceProxy
      .componentProcPause(this.pipe.id)
      .subscribe((result) => {
        this.notify.success("工序暂停成功");
        this.reloadProcInfo();
      });
  }

  finish() {
    this._taskServiceProxy
      .componentProcFinish(this.pipe.id,this.pipe.nodeId,this.checked)
      .subscribe((result) => {
        this.notify.success("工序完成成功");
        this.reloadProcInfo();
      });
  }

  restart() {
    this._taskServiceProxy
      .componentProcReInit(this.pipe.id)
      .subscribe((result) => {
        this.notify.success("工序重置成功");
        this.reloadProcInfo();
      });
  }
}
